# 第1阶段：使用 Node 构建前端项目
FROM node:20-alpine as builder

# 设置工作目录
WORKDIR /app

# 拷贝 package.json 和 package-lock.json 并安装依赖
COPY package*.json ./
RUN npm install --registry=https://registry.npmmirror.com

# 拷贝源代码并构建
COPY . .
RUN npm run build

# 第2阶段：使用 Nginx 提供服务
FROM nginx:alpine

# 删除默认配置
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d/default.conf

# 删除默认网页
RUN rm -rf /usr/share/nginx/html/*

# 拷贝构建好的前端资源
COPY --from=builder /app/dist /usr/share/nginx/html

# 设置工作目录
WORKDIR /usr/share/nginx/html

# 暴露端口
EXPOSE 80

# 启动 nginx
CMD ["nginx", "-g", "daemon off;"]
